package com.hcb.tonguetiphealth.widget.homepage;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.hcb.tonguetiphealth.adapter.TrendsListAdapter;
import com.hcb.tonguetiphealth.global.Global;
import com.hcb.tonguetiphealth.manager.JSONManager;
import com.hcb.tonguetiphealth.model.Trends;
import com.hcb.tonguetiphealth.util.HttpUtils;
import com.hcb.tonguetiphealth.util.MessageWhat;
import com.hcb.tonguetiphealth.util.StringUtils;
import com.hcb.tonguetiphealth.view.R;
import com.hcb.tonguetiphealth.widget.GeneralWidget;

public class HomepageFriendsTrendsWidget extends GeneralWidget implements OnClickListener {
	private static final String TAG = "HomepageOrderedInfoWidget";
	private String url = Global.URL + Global.servlet_friendsTrends;
	
	private Activity ctx;
	private Handler mHandler;
	private LinearLayout ll_back;
	private ListView listView;
	private TrendsListAdapter trendsListAdapter;
	private List<Trends> trendsList;
	
	public HomepageFriendsTrendsWidget(Context context) {
		super(context);
		ctx = (Activity)context;
		inflate(context, R.layout.homepage_personal_friendstrends, this); 
		
		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch(msg.what) {
				case MessageWhat.SHOW_DIALOG:
					showMessageDialog(ctx, "正在获取享友动态...");
					break;
				case MessageWhat.DIMISS_DIALOG:
					trendsListAdapter.notifyDataSetChanged();
					dismissMessageDialog();
					break;
				}
			}
		};

		listView = (ListView)findViewById(R.id.homepage_personal_friendstrends_listview);
		ll_back = (LinearLayout)findViewById(R.id.homepage_personal_friendstrends_back_button);
		ll_back.setOnClickListener(this);

		initData();
	}
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.homepage_personal_friendstrends_back_button:
			ctx.setContentView(new HomepagePersonalWidget(ctx));
			break;
		default:
			break;
		}
	}
	
	private void initData() {
		trendsList = new ArrayList<Trends>();
		trendsListAdapter = new TrendsListAdapter(ctx, trendsList);
		listView.setAdapter(trendsListAdapter);
		
		downTrends();
	}
	
	private void downTrends() {
		mHandler.sendEmptyMessage(MessageWhat.SHOW_DIALOG);
		new Thread() {
			public void run() {
				trendsList.clear();
				String response = null;
				try {
					List <NameValuePair> params = new ArrayList <NameValuePair>();
					params.add(new BasicNameValuePair("userId", Global.user.getUserId()));
					response = HttpUtils.Post(url, params);
					
					if(Global.DEBUG) {
						System.out.println("downTrends -----------------------------\n" + response);
					}
				} catch(Exception e) {
					Log.e(TAG, "Exception", e);
				}
				if(!StringUtils.isEmpty(response)) {
					JSONManager jManager = new JSONManager();
					boolean isGotJSONObject = jManager.isGotJSONObject(response);
					if(isGotJSONObject) {
						JSONArray jList = jManager.getJsonList();
						Trends trends;
						if(null == jList) {
							
						} else {
							JSONObject jObject;
							for(int i=0; i< jList.length(); i++) {
								jObject = getJSONObject(jList, i);
								if(null != jObject) {
									trends = new Trends();
									trends.setId(getString(jObject, "userid"));
									trends.setName(getString(jObject, "truename"));
									trends.setTime(getString(jObject, "addtime"));
									//trends.setContent(getString(jObject, "content"));
									trends.setContentWine(getString(jObject, "wine "));
									trends.setContentMeal(getString(jObject, "meal"));
									trends.setContentCate(getString(jObject, "cate "));
									trends.setImgURL(getString(jObject, "headicon"));

									trendsList.add(trends);
								}
							}
						}
						int count = jManager.getResult();
						String msg = jManager.getMsg();
						System.out.println("------ 得到msg: " + count + ":" + msg);
					}
				}
				mHandler.sendEmptyMessage(MessageWhat.DIMISS_DIALOG);
			}
		}.start();
	}
	
	@Override
	public void notifyAdapter() {
		
	}
}
